package leecode.q496;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class Solution {
    public void solve() {

    }

    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        // 计算数组2的每个元素的下一个最大元素
        Map<Integer, Integer> num2Map = new HashMap<>();
        Stack<Integer> stack = new Stack<>();
        for (int elem : nums2) {
            while (!stack.isEmpty() && elem > stack.peek()) {
                Integer pop = stack.pop();
                num2Map.put(pop, elem);
            }
            stack.push(elem);
        }
        int[] ints = new int[nums1.length];
        for (int i1 = 0; i1 < nums1.length; i1++) {
            Integer integer = num2Map.get(nums1[i1]);
            ints[i1] = integer == null ? -1 : integer;
        }
        return ints;
    }
}
